package com.example.administrator.demo_weather;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    private EditText et1;
    private TextView tv1;
    private TextView tv2;
    private ProgressDialog progressDialog;
    private static String TAG="test";
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            progressDialog.dismiss();
            switch (msg.what){
                case 1:
                    JSONArray list= (JSONArray) msg.obj;
                    try {
//                        JSONObject today=list.getJSONObject(0);
//                        Log.i(TAG,today.getString("date").toString());
//                        Log.i(TAG,list.getJSONObject(1).toString());

                        tv1.setText(list.getJSONObject(0).getString("high").toString());
                        tv2.setText(list.getJSONObject(0).getString("high").toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 0:
                    Toast.makeText(MainActivity.this,"获取失败",0).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=findViewById(R.id.et1);
        tv1=findViewById(R.id.tv1);
        tv2=findViewById(R.id.tv2);

    }
    public void search(View view){
        final String cityname=et1.getText().toString().trim();
        progressDialog=new ProgressDialog(MainActivity.this);
//        progressDialog.setMessage("正在获取天气数据...");
//        progressDialog.show();
        new Thread(){
            public void run(){
                try {
                    URL url=new URL("https://www.sojson.com/open/api/weather/json.shtml?city="+ URLEncoder.encode(cityname,"utf-8"));
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(3000);
                    int code=conn.getResponseCode();
                    if (code==200){
                        InputStream is=conn.getInputStream();
                        Log.i(TAG,"得到输入流开始解析json，先将返回的结果转化为字符串并保存");
                        StreamTools streamTools=new StreamTools();
                        String result=streamTools.readStream(is);
                        JSONObject jsonObject=new JSONObject(result);
                        String status=jsonObject.getString("status");
                        Log.i(TAG,status);
                        if ("200".equals(status)){
                            Log.i(TAG,"开始解析");
                            JSONObject jsondata=jsonObject.getJSONObject("data");
                            JSONArray list=jsondata.getJSONArray("forecast");
//                            JSONObject today=(JSONObject) list.getJSONObject(0);
//                            JSONObject tomorrow=(JSONObject) list.getJSONObject(1);
                            Message msg=Message.obtain();
                            msg.what=1;
                            msg.obj=list;
                            handler.sendMessage(msg);
                        }
                        is.close();
                    }else{
                        Message msg=Message.obtain();
                        msg.what=0;
                        handler.sendMessage(msg);
                        Log.i(TAG,"发生错误");
                    }
                } catch (Exception e) {
                    Log.i(TAG,"发生异常"+e);
                }
            }
        }.start();

    }
}
